package projects;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

@SuppressWarnings("serial")
public class interactionObjects implements Serializable {
	
	@SuppressWarnings("rawtypes")
	public Hashtable<Integer, String> queryClients(Integer clientTypeId){
		Connection con = null;
		Hashtable<Integer, String> clients = new Hashtable<Integer, String>();
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/projectsDb","root", "root");
			
			PreparedStatement query = (PreparedStatement) con.prepareStatement("select clientId , name from clients where clientTypeId = ?");
			query.setInt(1, clientTypeId);
			ResultSet rs = query.executeQuery();
			
			while(rs.next()){
				clients.put(rs.getInt(1), rs.getString(2));
				}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return clients;
	}

	@SuppressWarnings("rawtypes")
	public Hashtable queryEmployees(Integer clientTypeId){
		Connection con = null;
		Hashtable<Integer, String> clients = new Hashtable<Integer, String>();
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/projectsDb","root", "root");
			
			PreparedStatement query = (PreparedStatement) con.prepareStatement("select clientId , name from clients where clientTypeId = ?");
			query.setInt(1, clientTypeId);
			ResultSet rs = query.executeQuery();
			
			while(rs.next()){
				clients.put(rs.getInt(1), rs.getString(2));
				}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return clients;
	}
	
	public static void main(String args[]){
		interactionObjects x = new interactionObjects();
		Hashtable<Integer, String> returnedClients = new Hashtable<Integer, String>();
		returnedClients = x.queryClients(2);
		
		Enumeration<Integer> keys = returnedClients.keys();

		ArrayList<Integer> test = new ArrayList<Integer>();
		while(keys.hasMoreElements()){
			test.add(keys.nextElement());
		}

		for(Integer tst : test){
			System.out.println(returnedClients.get(tst));
		}
	}
}
